﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BaHuan.BusinessLayer;
using BaHuan.Common;
using BaHuan.Entities;
namespace BaHuan
{
    public partial class FrmKiemTraPhieuXuatHang : Form
    {
        private ChiNhanhKhachHangManagement chinhanhkhachangMgt = new ChiNhanhKhachHangManagement();
        private ChiNhanhCongTyMgt chinhanhctyMgt = new ChiNhanhCongTyMgt();
        private KhachHangManagement khachhangMgt = new KhachHangManagement();
        private PhieuXuatHangManagement phieuxuathangMgt = new PhieuXuatHangManagement();
        private CTPhieuXuatHangManagement ctphieuxuathangMgt = new CTPhieuXuatHangManagement();
        private PhieuXuatHang mphieuxuathang;

        public PhieuXuatHang PhieuXuatHang
        {
            set { this.mphieuxuathang = value; }
            get { return this.mphieuxuathang; }
        }

        public FrmKiemTraPhieuXuatHang()
        {
            InitializeComponent();
        }

        private void FrmKiemTraPhieuXuatHang_Load(object sender, EventArgs e)
        {
            LoadAll();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public void LoadAll()
        {
            if (this.mphieuxuathang != null)
            {
                dgvChiTiet.AutoGenerateColumns = false;

                cboKhachHang.DataSource = null;
                cboKhachHang.DataSource = GetAllKhachHang();
                cboKhachHang.DisplayMember = "Ten";
                cboKhachHang.ValueMember = "Id";

                cboChiNhanhCongTy.DataSource = null;
                cboChiNhanhCongTy.DataSource = GetAllChiNhanhCongTy();
                cboChiNhanhCongTy.DisplayMember = "TenChiNhanh";
                cboChiNhanhCongTy.ValueMember = "Id";

                cboChiNhanhKhachHang.DataSource = GetAllChiNhanhKhachHang();
                cboChiNhanhKhachHang.DisplayMember = "Ten";
                cboChiNhanhKhachHang.ValueMember = "Id";

                txtMaPhieu.Text = this.mphieuxuathang.MaSo;
                dtpNgayThang.Value = this.mphieuxuathang.NgayThang;
                txtNguoiLap.Text = this.mphieuxuathang.NguoiLap;

                cboKhachHang.Text = this.mphieuxuathang.KhachHang;
                txtMaSoThue.Text = this.mphieuxuathang.MaSoThue;

                cboChiNhanhKhachHang.Text = this.mphieuxuathang.ChiNhanhKhachHang;
                cboHTTT.Text = this.mphieuxuathang.HinhThucThanhToan;
                txtDiaChiGiaoHang.Text = this.mphieuxuathang.DiaChi;
                cboChiNhanhCongTy.SelectedValue = this.mphieuxuathang.IdChiNhanhCongTy;
                txtSoHDD.Text = string.IsNullOrEmpty(this.mphieuxuathang.SoHoaDonDo) ? "" : this.mphieuxuathang.SoHoaDonDo.Substring(7);
                txtTongTienHang.Text = this.mphieuxuathang.TongTienHang.ToString();
                txtChietKhau.Text = this.mphieuxuathang.ChietKhau.ToString();
                txtTienChietKhau.Text = this.mphieuxuathang.TienChietKhau.ToString();
                txtVAT.Text = this.mphieuxuathang.VAT.ToString();
                txtTienVAT.Text = this.mphieuxuathang.TienVAT.ToString();
                txtTongCong.Text = this.mphieuxuathang.TongCong.ToString();

                List<CTPhieuXuatHang> lstCTPhieuXuatHang = (from ctpx in GetAllCTPhieuXuatHang()
                                                            where ctpx.IdPhieuXuatHang == this.mphieuxuathang.Id
                                                            select ctpx).ToList();

                dgvChiTiet.DataSource = lstCTPhieuXuatHang;
                long tienbaobi = 0;
                for (int i = 0; i < dgvChiTiet.Rows.Count; i++)
                {
                    string sCompate = dgvChiTiet.Rows[i].Cells[1].Value.ToString().Substring(0, 1).ToLower();
                    if (sCompate == "b" || sCompate == "8" || sCompate == "9")
                        tienbaobi += Convert.ToInt64(dgvChiTiet.Rows[i].Cells[5].Value.ToString());
                }
                txtTienBaoBi.Text = tienbaobi.ToString();
            }
        }

        public List<KhachHang> GetAllKhachHang()
        {
            ResultObj resultObj = new ResultObj();
            resultObj = khachhangMgt.GetAllKhachHang();
            List<KhachHang> listKhachHang = (List<KhachHang>)resultObj.Result;

            return listKhachHang;
        }

        public List<ChiNhanhKhachHang> GetAllChiNhanhKhachHang()
        {
            ResultObj resultObj = new ResultObj();
            resultObj = chinhanhkhachangMgt.GetAllChiNhanhKhachHang();
            List<ChiNhanhKhachHang> listChiNhanhKhachHang = (List<ChiNhanhKhachHang>)resultObj.Result;

            return listChiNhanhKhachHang;
        }

        public List<ChiNhanhCongTy> GetAllChiNhanhCongTy()
        {
            ResultObj resultObj = new ResultObj();
            resultObj = chinhanhctyMgt.GetAllChiNhanhCongTy();
            List<ChiNhanhCongTy> listChiNhanhCongTy = (List<ChiNhanhCongTy>)resultObj.Result;

            return listChiNhanhCongTy;
        }

        public List<CTPhieuXuatHang> GetAllCTPhieuXuatHang()
        {
            ResultObj resultObj = new ResultObj();
            resultObj = ctphieuxuathangMgt.GetAllCTPhieuXuatHang();
            List<CTPhieuXuatHang> listCTPhieuXuatHang = (List<CTPhieuXuatHang>)resultObj.Result;

            return listCTPhieuXuatHang;
        }

        public List<PhieuXuatHang> GetAllPhieuXuatHang()
        {
            ResultObj resultObj = new ResultObj();
            resultObj = phieuxuathangMgt.GetAllPhieuXuatHang();
            List<PhieuXuatHang> listPhieuXuatHang = (List<PhieuXuatHang>)resultObj.Result;

            return listPhieuXuatHang;
        }
    }
}
